package leetcode.Leetcode15;

import java.util.*;

class DraftSolution {
    //超时
    public List<List<Integer>> threeSum(int[] nums) {
        //从第一个开始遍历，判断里面是否存在三个相加为0的数
        Map<String,List<Integer>> listHashMap = new HashMap<>();

        for (int i = 0; i < nums.length; i++) {
            for (int j = i+1; j < nums.length; j++) {
                for (int k = j+1; k < nums.length; k++) {
                    if ((nums[i]+nums[j]+nums[k])==0) {
                        int[] array = {nums[i], nums[j], nums[k]};
                        //排序后的三个值拼接后作为key
                        for (int l = 0; l < array.length-1; l++) {
                            for (int m = 0; m < array.length-1-l; m++) {
                                if (array[m]>array[m+1]) {
                                    int tmp = array[m];
                                    array[m]=array[m+1];
                                    array[m+1]=tmp;
                                }
                            }
                        }
                        for (int l = 0; l < array.length; l++) {
                            System.out.print(String.valueOf(array[l])+' ');
                        }
                        StringBuffer key = new StringBuffer();
                        for (int l = 0; l < array.length; l++) {
                            key.append(String.valueOf(array[l]));
                        }
                        System.out.println(key);
                        ArrayList<Integer> list = new ArrayList<>();
                        list.add(nums[i]);
                        list.add(nums[j]);
                        list.add(nums[k]);
                        listHashMap.put(String.valueOf(key), list);
                    }
                }
            }
        }
        return new ArrayList<List<Integer>>(listHashMap.values());
    }

    public static void main(String[] args) {
        DraftSolution draftSolution = new DraftSolution();
        int[] test=new int[]{-1,0,1,2,-1,-4};
        List<List<Integer>> lists = draftSolution.threeSum(test);
        System.out.println(lists);

    }
}